package com.tjsinfo.tjsoa.schedule.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.schedule.bo.IScheduleMainBo;
import com.tjsinfo.tjsoa.schedule.vo.ScheduleMain;
import com.tjsinfo.tjsoa.system.vo.User;


public class ScheduleWeekAction extends Action{

	private IScheduleMainBo scheduleBo;

	public IScheduleMainBo getScheduleBo() {
		return scheduleBo;
	}

	public void setScheduleBo(IScheduleMainBo scheduleBo) {
		this.scheduleBo = scheduleBo;
	}
	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Date date = null;
		String time = (String) request.getParameter("time");
		request.setAttribute("nowtime", time);
		ScheduleMain main = new ScheduleMain();
	

		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = dateFormat.parse(time);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 周日
		String sunday = ToolKit.getSunday(date);
		String[] splitsunday = sunday.split("-");
		main.setScheduleYear(Integer.valueOf(splitsunday[0]));
		main.setScheduleMonth(Integer.valueOf(splitsunday[1]));
		main.setScheduleDay(Integer.valueOf(splitsunday[2]));
		main.setPeopleId(user.getUser_id());
		
		List sundaylist = scheduleBo.findByDate(main);
		request.setAttribute("sundaylist", sundaylist);
		request.setAttribute("sunday", sunday);
		// 周一
		String monday = ToolKit.getMonday(date);
		String[] splitmonday = monday.split("-");
		main.setScheduleYear(Integer.valueOf(splitmonday[0]));
		main.setScheduleMonth(Integer.valueOf(splitmonday[1]));
		main.setScheduleDay(Integer.valueOf(splitmonday[2]));
		main.setPeopleId(user.getUser_id());
		List mondaylist = scheduleBo.findByDate(main);
		request.setAttribute("mondaylist", mondaylist);
		request.setAttribute("monday", monday);
		// 周二
		String tuesday = ToolKit.getTuesday(date);
		String[] splittuesday = tuesday.split("-");
		main.setScheduleYear(Integer.valueOf(splittuesday[0]));
		main.setScheduleMonth(Integer.valueOf(splittuesday[1]));
		main.setScheduleDay(Integer.valueOf(splittuesday[2]));
		main.setPeopleId(user.getUser_id());
		List tuesdaylist = scheduleBo.findByDate(main);

		request.setAttribute("tuesdaylist", tuesdaylist);
		request.setAttribute("tuesday", tuesday);
		
		// 周三
		String wednesday = ToolKit.getWednesday(date);
		
		String[] splitwednesday = wednesday.split("-");
		main.setScheduleYear(Integer.valueOf(splitwednesday[0]));
		main.setScheduleMonth(Integer.valueOf(splitwednesday[1]));
		main.setScheduleDay(Integer.valueOf(splitwednesday[2]));
		main.setPeopleId(user.getUser_id());
		List wednesdaylist = scheduleBo.findByDate(main);
		request.setAttribute("wednesdaylist", wednesdaylist);
		request.setAttribute("wednesday", wednesday);
		// 周四
		String thursday = ToolKit.getThursday(date);
		
		String[] splitthursday = thursday.split("-");
		main.setScheduleYear(Integer.valueOf(splitthursday[0]));
		main.setScheduleMonth(Integer.valueOf(splitthursday[1]));
		main.setScheduleDay(Integer.valueOf(splitthursday[2]));
		main.setPeopleId(user.getUser_id());
		List thursdaylist = scheduleBo.findByDate(main);
		request.setAttribute("thursdaylist", thursdaylist);
		request.setAttribute("thursday", thursday);
		// 周五
		String friday = ToolKit.getFriday(date);
		
		String[] splitfriday = friday.split("-");
		main.setScheduleYear(Integer.valueOf(splitfriday[0]));
		main.setScheduleMonth(Integer.valueOf(splitfriday[1]));
		main.setScheduleDay(Integer.valueOf(splitfriday[2]));
		main.setPeopleId(user.getUser_id());
		List fridaylist = scheduleBo.findByDate(main);
		request.setAttribute("fridaylist", fridaylist);
		request.setAttribute("friday", friday);
		// 周六
		String saturday = ToolKit.getSaturday(date);
		String[] splitsaturday = saturday.split("-");
		 main.setScheduleYear(Integer.valueOf(splitsaturday[0]));
		 main.setScheduleMonth(Integer.valueOf(splitsaturday[1]));
		 main.setScheduleDay(Integer.valueOf(splitsaturday[2]));
		 main.setPeopleId(user.getUser_id());
		 List saturdaylist = scheduleBo.findByDate(main);
		 request.setAttribute("saturdaylist", saturdaylist);
		 request.setAttribute("saturday", saturday);
		 
		 
		 return mapping.findForward("ok");
	}
}
